<template>
  <div class="btn text-xs flex items-center justify-between">
    <div>
      <el-button round :icon="PlayOne">播放全部</el-button>
      <el-button round :icon="Download">下载</el-button>
      <el-button round :icon="CopyLink">批量操作</el-button>
    </div>
    <div class="flex gap-x-5 mr-10">
      <span class="icon-hover" :class="{ 'text-emerald-400': state.order === 'hot' }" @click="setOrder('hot')">
        最热
      </span>
      <span class="icon-hover" :class="{ 'text-emerald-400': state.order === 'time' }" @click="setOrder('time')">
        最新
      </span>
    </div>
  </div>
  <div class="mt-2">
    <div class="flex text-xs text-gray-400 py-2">
      <div class="flex-auto">歌曲</div>
      <div class="w-1/3">专辑</div>
      <div class="w-20">时长</div>
    </div>
    <template v-for="item in state.list" :key="item.id">
      <SongItem :info="item" />
    </template>
    <div v-if="state.hasMore" class="text-center">
      <el-button class="mx-auto" type="success" link @click="loadMore" v-loading="state.isLoading">
        加载更多
      </el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import type { ArtistSongQuery } from '@/types/common/common'
import type { Song } from '@/types/artist/index'
import { getArtistSongs } from '@/api/artist'
import { PlayOne, Download, CopyLink } from '@icon-park/vue-next'
import SongItem from './SongItem.vue'

const props = defineProps<{
  id: number
}>()

const state = reactive({
  order: 'hot',
  limit: 20,
  page: 1,
  list: [] as Song[],
  total: 0,
  hasMore: false,
  isLoading: false,
})

const getSongList = async () => {
  const params: ArtistSongQuery = {
    order: state.order,
    limit: state.limit,
    offset: (state.page - 1) * state.limit,
    id: props.id,
  }
  const { data } = await getArtistSongs(params)
  if (data.code !== 200) return ElMessage.error('获取失败')
  state.list = [...state.list, ...data.songs]
  state.total = data.total
  state.hasMore = data.more
}

getSongList()

const setOrder = (type: string) => {
  state.page = 1
  state.order = type
  state.list = []
  getSongList()
}

const loadMore = () => {
  state.page++
  getSongList()
}
</script>

<style lang="less" scoped>
.el-button {
  @apply text-xs w-24;
}
</style>
